package com.plc.s7;

import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import com.github.s7connector.api.S7Connector;
import com.github.s7connector.api.factory.S7ConnectorFactory;
import com.github.s7connector.exception.S7Exception;

public class S7Client {

    Log log = LogFactory.get();
    public S7Connector initConnect(String ipAddress) throws NullPointerException{
        String now = DateUtil.now();
        S7Connector connector = null;
        try {
        //默认端口
        //创建与PLC的链接
             connector = S7ConnectorFactory.buildTCPConnector()
                    .withHost(ipAddress)
                    .withRack(0) // 架机号 可选
                    .withSlot(1) // 插槽号 可选
                    .build();
            return connector;
        }catch (S7Exception exception){
            log.log(Level.INFO,"日期{},调用plc地址为：{},失败,无法连接至PLC服务器",now,ipAddress);
        }
        return null;
    }
}
